package vn.buffalocoders.rikos.ui.common.sessions;

import java.util.HashMap;

import vn.buffalocoders.rikos.ui.home.HomeActivity;
import vn.buffalocoders.rikos.ui.login.LogInActivity;
import vn.buffalocoders.rikos.ui.network.ConnectRikoServer;
import vn.buffalocoders.rikos.ui.network.LogoutTask;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
import android.util.Log;

public class SessionManager {
    // Shared Preferences
    SharedPreferences pref;
     
    // Editor for Shared preferences
    Editor editor;
     
    // Context
    Context _context;
     
    // Shared pref mode
    int PRIVATE_MODE = 0;
     
    // Sharedpref file name
    private static final String PREF_NAME 	= "BuffaloCoderVNRikos";
     
    // All Shared Preferences Keys
    // User name (make variable public to access from outside)
    public static final String KEY_NAME 		= "name";
     
    // Email address (make variable public to access from outside)
    public static final String KEY_EMAIL 		= "email";
    
    public static final String KEY_PASS 		= "pass";
    
    public static final String KEY_AUTH_TOKEN 	= "AuthToken";
    
    public static final String KEY_USER_ID 		= "UserId";
    
    public static final String KEY_AVATAR		= "Avatar";
    
    // Constructor
    public SessionManager(Context context){
        this._context = context;
        pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
        editor = pref.edit();
    }
    
    /**
    * Create login session
    **/
   public void createLoginSession(String name, String email, String password, String authToken, String userId, String avatar){
       // Storing name in pref
       editor.putString(KEY_NAME, name);
        
       // Storing email in pref
       editor.putString(KEY_EMAIL, email);
       
       editor.putString(KEY_PASS, password);
       
       editor.putString(KEY_AUTH_TOKEN, authToken);
       
       editor.putString(KEY_USER_ID, userId);
       
       if (!TextUtils.isEmpty(avatar)) {
    	   editor.putString(KEY_AVATAR, avatar);
       }
       // commit changes
       editor.commit();
   }
   
   /**
    * Get stored session data
    * */
   public HashMap<String, String> getUserDetails(){
       HashMap<String, String> user = new HashMap<String, String>();
       // user name
       user.put(KEY_NAME, pref.getString(KEY_NAME, null));
       // user email id
       user.put(KEY_EMAIL, pref.getString(KEY_EMAIL, null));
        
       user.put(KEY_PASS, pref.getString(KEY_PASS, null));
       
       user.put(KEY_AUTH_TOKEN, pref.getString(KEY_AUTH_TOKEN, null));
       
       user.put(KEY_USER_ID, pref.getString(KEY_USER_ID, null));
       
   	   user.put(KEY_AVATAR, pref.getString(KEY_AVATAR, null));
       // return user
       return user;
   }

   public String getUsername() {
	   return pref.getString(KEY_NAME, null);
   }
   
   public String getEmail() {
	   return pref.getString(KEY_EMAIL, null);
   }
   
   public String getPassword() {
	   return pref.getString(KEY_PASS, null);
   }
   
   public String getAuthToken() {
	   return pref.getString(KEY_AUTH_TOKEN, null);
   }
   
   public String getUserId() {
	   return pref.getString(KEY_USER_ID, null);
   }
   
   public String getAvatar() {
	   return pref.getString(KEY_AVATAR, null);
   }
   
   /**
    * Clear session details
    * */
   public void logoutUser(){
       // Clearing all data from Shared Preferences
       editor.clear();
       editor.commit();
       
       new LogoutTask().execute();

       // After logout redirect user to Loing Activity
       //Intent i = new Intent(_context, HomeActivity.class);
       // Closing all the Activities
       // i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        
       // Add new Flag to start new Activity
       // i.setFlags(Intent.FLAG_ACTIVITY_TASK_ON_HOME);
        
       // Staring Login Activity
       // _context.startActivity(i);
   }
   
   public void clearData() {
       editor.clear();
       editor.commit();	   
   }
   
   /**
    * Check login method wil check user login status
    * If false it will redirect user to login page
    * Else won't do anything
    * */
   public void checkLogin(){
       // Check login status
	   Log.i("LETRUONG", "checklogin");
       if(!this.isLoggedIn()){
           // user is not logged in redirect him to Login Activity
           Intent i = new Intent(_context, LogInActivity.class);
           // Closing all the Activities
           i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            
           // Add new Flag to start new Activity
           i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
           // Staring Login Activity
           _context.startActivity(i);
       }
   }
    
   /**
    * Quick check for login
    * **/
   // Get Login State
   public boolean isLoggedIn(){
      String username = pref.getString(KEY_EMAIL, null);
      String password = pref.getString(KEY_PASS, null);
      Log.i("LETRUONG", String.format("username=%s,pass=%s", username, password));
      return !TextUtils.isEmpty(username)  && !TextUtils.isEmpty(password);
   }
}